package com.autohammal.ui.frame.data;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;

import com.autohammal.ITarget;

public class ItemsCompositeViewerContainer extends
		ViewerContainer<ItemsCompositeViewer> {
	private Map<Class<? extends ITarget>, Integer> cache = new HashMap<Class<? extends ITarget>, Integer>();

	public ItemsCompositeViewer newItemsCompositeViewer(
			Class<? extends ITarget> clz) {
		Integer index = cache.get(clz);
		ItemsCompositeViewer viewer = null;
		if (index == null) {
			viewer = super.newViewer(ItemsCompositeViewer.class);
			cache.put(clz, proxy.size() - 1);
		} else {
			viewer = get(index);
		}
		return viewer;
	}

	@Override
	public ItemsCompositeViewer remove(int index) {
		ItemsCompositeViewer viewer = super.remove(index);
		if (viewer != null) {
			for (Iterator<Entry<Class<? extends ITarget>, Integer>> iterator = cache
					.entrySet().iterator(); iterator.hasNext();) {
				Entry<Class<? extends ITarget>, Integer> entry = iterator
						.next();
				if (entry.getValue() == index) {
					iterator.remove();
				}
			}
		}
		return viewer;
	}

	@Override
	public void clear() {
		super.clear();
		cache.clear();
	}
}
